import {DateTimeFormatter, OffsetDateTime, YearMonth, ZoneId, ZoneOffset} from "@js-joda/core";
import {Locale} from "@js-joda/locale_zh";
import dayjs from 'dayjs';

import 'dayjs/locale/zh-cn';
import {eachAnyIfIsObject} from "./ObjectUtils";

dayjs.locale('zh-cn');
export const YEAR_MONTH_DAY_FORMATER = DateTimeFormatter.ofPattern('YYYY年M月d日').withLocale(Locale.CHINESE)
export const DATE_WITH_TIME_FORMATER = DateTimeFormatter.ofPattern('YYYY年M月d日 H时mm分ss秒').withLocale(Locale.CHINESE)


export function preHandleObjectWithDateTimeParser(object:any){
    return eachAnyIfIsObject(object,(value, key, parent) => {

        if (typeof key === "string") {
            if (key.endsWith("DateTime")) {
                if (typeof value === "string"){
                    parent[key] = OffsetDateTime.parse(value)
                        .atZoneSameInstant(ZoneId.systemDefault())
                        .toOffsetDateTime()
                }
            }else if(key.endsWith("YearMonth")){
                parent[key] = YearMonth.parse(value)
            }

        }
    })
}